package com.jlect.swebing.ui.client;

import com.jlect.swebing.renderers.client.*;
import com.jlect.swebing.util.client.Strings;

/**
 * Base class for all text based components
 *
 * @author Sergey Kozmin
 * @since 21.11.2007 9:43:47
 */
public abstract class TextComponentsBase extends GComponent implements RendererActionListener {
    private ActionListenerCollection actionListeners;

    public TextComponentsBase(String text) {
        if(!Strings.isEmpty(text)) {
            getTextFieldRenderer().setText(text);
        }
    }

    public TextComponentsBase() {
        this("");
    }

    public void setText(String text) {
        getTextFieldRenderer().setText(text);
    }

    public String getText() {
        return getTextFieldRenderer().getText();
    }

    public void addActionListener(ActionListener listener) {
        getActionListener().add(listener);
    }

    public void removeActionListener(ActionListener listener) {
        getActionListener().remove(listener);
    }

    protected TextFieldRenderer getTextFieldRenderer() {
        return (TextFieldRenderer) getRenderer();
    }

    private ActionListenerCollection getActionListener() {
        if (actionListeners == null) {
            actionListeners = new ActionListenerCollection();
        }
        return actionListeners;
    }

    public void fireActionPerformed(RendererEvent event) {
        getActionListener().textActionPerformed(this);
    }

    protected Renderer createRenderer() {
        TextRenderer textRenderer = createTextRenderer();
        textRenderer.setActionListener(this);
        return textRenderer;
    }
    
    protected abstract TextRenderer createTextRenderer();
}
